package com.ybkj.daijia.api.apirest;

import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller("ApiSystemProcessController")
@RequestMapping(value = "api/system/")
public class SystemProcessController {

    private static final Logger logger = LoggerFactory.getLogger(SystemProcessController.class);

    @Autowired
    private SettingUtils settingUtils;


    /**
     * 获取是否开启跑腿定价
     *
     * @return
     */
    @RequestMapping(value = "getAppSetting", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> findByCity() {
        Setting setting = settingUtils.get();

        Map<String, Object> map = new HashMap<String, Object>();

        map.put("errandFixPrice", setting.isPaotuiFixPrice());
        map.put("nearPaotuiOrdersKm", setting.getNearPaotuiOrdersKm());
        map.put("zhuanchePrePay", setting.isZhuanchePrePay());
        map.put("daijiaPrePay", setting.isDaijiaPrePay());
        map.put("errandGrabAmount", setting.getErrandGrabAmount());
        map.put("errandValuationMethod", setting.getErrandValuationMethod());
        map.put("bookTimeZhuanxian", setting.getBookTimeZhuanxian());
        map.put("bookTimeZhuan", setting.getBookTimeZhuan());
        map.put("paotuiPrePay", setting.isPaotuiPrePay());
        map.put("yuyueMax", setting.getZhuanYuyueMax());
        map.put("yuyueTime", setting.getBookTimeZhuan());
        map.put("zhuanYuyue", setting.getZhuanYuyueTime());

        return map;
    }


    /**
     * 获取专车预约时间
     *
     * @return
     */
    @RequestMapping(value = "getYuyueSetting", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> findByYuyue() {
        Setting setting = settingUtils.get();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("yuyueMax", setting.getZhuanYuyueMax());
        map.put("zhuanYuyue", setting.getZhuanYuyueTime());
        return map;
    }

    /**
     * 获取开放的业务
     *
     * @return
     */
    @RequestMapping(value = "getService", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> getService() {
        Map<String, Object> serviceMap = new HashMap<String, Object>();
        try {
            Properties prop = PropertiesLoaderUtils.loadAllProperties("config.properties");
            String daijia = prop.getProperty("function.daijia.open");
            String zhuanche = prop.getProperty("function.zhuanche.open");
            String gasstation = prop.getProperty("function.gasstation.open");
            String zuche = prop.getProperty("function.zuche.open");
            String paotui = prop.getProperty("function.paotui.open");
            String freight = prop.getProperty("function.freight.open");
            String partner = prop.getProperty("function.partner.open");
            String zhuanxian = prop.getProperty("function.zhuanxian.open");
            serviceMap.put("daijia", Boolean.valueOf(daijia));
            serviceMap.put("zhuanche", Boolean.valueOf(zhuanche));
            serviceMap.put("zuche", Boolean.valueOf(zuche));
            serviceMap.put("paotui", Boolean.valueOf(paotui));
            serviceMap.put("partner", Boolean.valueOf(partner));
            serviceMap.put("freight", Boolean.valueOf(freight));
            serviceMap.put("zhuanxian", Boolean.valueOf(zhuanxian));
            serviceMap.put("gasstation", Boolean.valueOf(gasstation));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return serviceMap;
    }

    //查询订单
    @RequestMapping(value = "queryPaySetting", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> queryPaySetting() {
        Map<String, Object> map = new HashMap<String, Object>();
        Boolean aliPay = true;
        Boolean weixinPay = true;
        Boolean unionPay = true;
        Boolean bestPay = true;
        Setting setting = settingUtils.get();
        if (StringUtils.isBlank(setting.getAlipayPartnerID()) || StringUtils
            .isBlank(setting.getAlipaySellerID()) || StringUtils
            .isBlank(setting.getAlipayPartnerPrivKey())) {
            aliPay = false;
        }
        if (StringUtils.isBlank(setting.getWxAppID()) || StringUtils
            .isBlank(setting.getWxAppSecret()) || StringUtils.isBlank(setting.getWxPartnerID())
            || StringUtils.isBlank(setting.getWxPartnerKey())) {
            weixinPay = false;
        }
        if (StringUtils.isBlank(setting.getMerId())) {
            unionPay = false;
        }
        if (StringUtils.isBlank(setting.getMerchantId()) || StringUtils.isBlank(setting.getMerKey())
            || StringUtils.isBlank(setting.getMchPwd()) || StringUtils
            .isBlank(setting.getRiskcontrolinfo())) {
            bestPay = false;
        }
        map.put("success", true);
        map.put("weixinPay", weixinPay);
        map.put("aliPay", aliPay);
        map.put("unionPay", unionPay);
        map.put("bestPay", bestPay);
        map.put("nearFreightDriversKm", setting.getNearFreightDriversKm());
        map.put("message", "查询接口成功");

        return map;
    }

    /***
     * 获取系统时间
     * @param response
     */
    @RequestMapping("getTimestamp")
    public void getTimestamp(HttpServletResponse response) {
        long l = System.currentTimeMillis();
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            writer.print(l);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            writer.close();
        }

    }
}
